<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    var arr = [1,3,2,18,4,15,22,9,7,8,6]
    // 冒泡排序
    function maopao(array){
      for(var i=0;i<array.length;i++){
        for(var j=i;j<array.length-i;j++){
          if(array[j]>array[j+1]){
            [array[j],array[j+1]] = [array[j+1],array[j]]
          }
        }
      }
      return array
    }
    maopao(arr)
  </script>

  <script>
    var arr = [1,3,2,18,4,15,22,9,7,8,6]
    // 快速排序
    function kuaisu(array){
      let len = array.length
      if(len<=1){
        return array
      }
      let middleIndex = Math.floor(len/2)
      let middleItem = array.splice(middleIndex,1)
      let left=[],right=[]
      for (item of array) {
        if(item<middleItem){
          left.push(item)
        }else{
          right.push(item)
        }
      }
      return kuaisu(left).concat(middleItem,kuaisu(right))
    }
    let res = kuaisu(arr)
    console.log('快速排序',res);
  </script>

  <script>
    var arr = [1,3,2,18,4,15,22,9,7,8,6]
    //出入排序
    function charu(array){
      let len = array.length
      let cur,pre
      for(let i=1;i<len;i++){
        cur = array[i]
        pre = i-1
        while(pre>-1 && cur<array[pre]){
          array[pre+1] = array[pre]
          pre--
        }
        array[pre+1] = cur
      }
      return array
    }
  </script>

</body>
</html>